/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.order;

import java.util.Collections;
import java.util.List;

import net.martinimix.dao.order.OrderDao;
import net.martinimix.domain.account.Customer;
import net.martinimix.domain.order.Cart;
import net.martinimix.domain.order.Order;
import net.martinimix.domain.order.OrderHeader;
import net.martinimix.domain.order.PlannedShipment;
import net.martinimix.domain.order.PriceList;

/**
 * Provides a MartiniMix order service.
 * 
 * @author Scott Rossillo
 *
 */
public class MartiniMixOrderService implements OrderService {

	/**
	 * The order data access object for this service.
	 */
	protected OrderDao orderDao;
	
	
	/* (non-Javadoc)
	 * @see net.martinimix.service.order.OrderService#calculateTaxRate(net.martinimix.domain.order.Order)
	 */
	public double calculateTaxRate(Order order) {
		return orderDao.calculateTaxRate(order);
	}

	/* (non-Javadoc)
	 * @see net.martinimix.service.order.OrderService#createOrder(net.martinimix.domain.account.Customer, net.martinimix.domain.order.Cart, net.martinimix.domain.order.PriceList)
	 */
	public Order createOrder(Customer customer, Cart cart, PriceList priceList) {
		return orderDao.createOrder(customer, cart, priceList);
	}

	/**
	 * Creates a single planned shipment using the given order's global ship method.
	 * 
	 * @see net.martinimix.service.order.OrderService#createPlannedShipments(net.martinimix.domain.order.Order)
	 */
	public List createPlannedShipments(Order order) {
		
		final OrderHeader orderHeader = order.getOrderHeader();
		final PlannedShipment plannedShipment = new PlannedShipment();
		
		plannedShipment.setPrimary(true);
		plannedShipment.setCurrencyCode(orderHeader.getCurrencyCode());
		plannedShipment.setAddressId(order.getShippingAddress().getId());
		plannedShipment.setOrderHeaderId(orderHeader.getId());
		plannedShipment.setShipMethodId(orderHeader.getShipMethodId());
		plannedShipment.setTotalShipAmount(orderHeader.getShipAmount());
		plannedShipment.setTotalAmount(orderHeader.getSubTotalPrice());
		
		return Collections.singletonList(plannedShipment);
	}
	

	/* (non-Javadoc)
	 * @see net.martinimix.service.order.OrderService#saveOrder(net.martinimix.domain.order.Order, net.martinimix.domain.order.Cart)
	 */
	public void saveOrder(Order order, Cart cart) {
		
		List plannedShipments = order.getPlannedShipments();
		
		if(order.getPlannedShipments() == null) {
			plannedShipments = this.createPlannedShipments(order);
			order.setPlannedShipment(plannedShipments);
		}
		
		orderDao.saveOrder(order, cart);
	}

	/**
	 * Sets the order data access object for this service.
	 * 
	 * @param orderDao the <code>OrderDao</code> for this service
	 */
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

}
